package cn.itcast.wushangxiantu.Activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.ZhuangbeiChongZhuGriveViewAdapter;
import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.UserData.UserWuQiChongZhu;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ZhuangBeiTuPo extends AppCompatActivity implements View.OnClickListener {

    private Button btnTuJianGW;
    private TextView tvTianJiaGW;
    private TextView tvHaveGW;
    private Button btnTuJianLS;
    private TextView tvTianJiaLS;
    private TextView tvHaveLS;
    private Button btnTuJianSH;
    private TextView tvTianJiaSH;
    private TextView tvHaveSH;
    private TextView tvName;
    private TextView tvDj;
    private TextView tvShengJi;
    private TextView tvJiangJ;
    private TextView tvTuPo;
    private TextView tvShuoMing;
    private GridView gvBag;
    private ListView tvLv;

    private int gwNumber = 0;//拥有数量
    private int lsNumber = 0;//拥有数量
    private int shNumber = 0;//拥有数量
    private int addgw;//添加数量
    private int addls;//添加数量
    private int addsh;//添加数量
    private int yesNumber = 20;//成功率
    private int noNumber = 100;//掉级率

    private int bagi;//背包栏id
    private int bag_id;//玩家背包id
    private ZhuangbeiChongZhuGriveViewAdapter adatper;
    private UserWuQiChongZhu userWuQiChongZhu;//武器重铸获取预览
    private RadioButton rbtn1;
    private RadioButton rbtn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuang_bei_tu_po);
        initView();
    }

    private void initView() {
        btnTuJianGW = (Button) findViewById(R.id.btnTuJianGW);
        tvTianJiaGW = (TextView) findViewById(R.id.tvTianJiaGW);
        tvHaveGW = (TextView) findViewById(R.id.tvHaveGW);
        btnTuJianLS = (Button) findViewById(R.id.btnTuJianLS);
        tvTianJiaLS = (TextView) findViewById(R.id.tvTianJiaLS);
        tvHaveLS = (TextView) findViewById(R.id.tvHaveLS);
        btnTuJianSH = (Button) findViewById(R.id.btnTuJianSH);
        tvTianJiaSH = (TextView) findViewById(R.id.tvTianJiaSH);
        tvHaveSH = (TextView) findViewById(R.id.tvHaveSH);
        tvName = (TextView) findViewById(R.id.tvName);
        tvDj = (TextView) findViewById(R.id.tvDj);
        tvShengJi = (TextView) findViewById(R.id.tvShengJi);
        tvJiangJ = (TextView) findViewById(R.id.tvJiangJ);
        tvTuPo = (TextView) findViewById(R.id.tvTuPo);
        tvTuPo.setOnClickListener(this);
        tvShuoMing = (TextView) findViewById(R.id.tvShuoMing);
        gvBag = (GridView) findViewById(R.id.gvBag);
        tvLv = (ListView) findViewById(R.id.tvLv);
        gvBag.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (null != userWuQiChongZhu) {
                    bagi = i;
                    bag_id = userWuQiChongZhu.getZhuangBei().get(i).getBag_id();
                    tvName.setText(userWuQiChongZhu.getZhuangBei().get(i).getWpname());
                    tvDj.setText("当前数值" + userWuQiChongZhu.getZhuangBei().get(i).getPz());
                    String content = "";
                    switch (userWuQiChongZhu.getZhuangBei().get(i).getWpregion()) {
                        case 2:
                            content += "攻击力：" + ((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * 40 +
                                    userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 50))) + "\n";
                            if (((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() / 5 +
                                    userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 1000)))>45){
                                content += "暴击率：45";
                            }else {
                                content += "暴击率：" + ((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() / 5 +
                                        userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 1000)));
                            }
                            break;
                        case 3:
                            content += "生命：" + ((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * 100 +
                                    userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 50))) + "\n";
                            content += "防御力：" + ((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * 20 +
                                    userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 100)));
                            break;
                        case 4:
                            if (((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() / 5 +
                                    userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 1000)))>45){
                                content += "命中：45\n";
                            }else {
                                content += "命中：" + ((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() / 5 +
                                        userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 1000))) + "\n";
                            }
                           content += "穿透：" + ((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * 10 +
                                    userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 50)));
                            break;
                        case 5:
                            if (((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() / 5 +
                                    userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 1000)))>45){
                                content += "闪避：45\n";
                            }else {
                                content += "闪避：" + ((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() / 5 +
                                        userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 1000))) + "\n";
                            }
                           content += "恢复：" + ((int) (userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * 10 +
                                    userWuQiChongZhu.getZhuangBei().get(i).getJsdj() * ((double) userWuQiChongZhu.getZhuangBei().get(i).getPz() / 50)));
                            break;
                    }
                    tvShuoMing.setText(content);
                }
            }
        });
        btnTuJianGW.setOnClickListener(this);
        btnTuJianLS.setOnClickListener(this);
        btnTuJianSH.setOnClickListener(this);

        ZhuangBeiYuLan(Login.getUser().getUsernumber());//获取装备
        rbtn1 = (RadioButton) findViewById(R.id.rbtn1);
        rbtn2 = (RadioButton) findViewById(R.id.rbtn2);
    }

    private void ZhuangBeiYuLan(String usernumer) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + usernumer + "&need=wuqichongzhuyulan", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("yulan1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("yulan2", src);
                UserWuQiChongZhu user = null;
                try {
                    user = new Gson().fromJson(src, UserWuQiChongZhu.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 1;
                handler.sendMessage(message);
            }
        });
    }

    private void ZhuangBeiChongZhu(String usernumer, int bag_id, int lsNumber, int gwNumber, int shNumber) {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userNumber=" + usernumer + "&need=wuqichongzhu" + "&bag_id=" + bag_id +
                "&gwnum=" + gwNumber + "&lsnum=" + lsNumber + "&shnum=" + shNumber, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("chongzhu1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("chongzhu2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch (message.what) {
                case 1:
                    UserWuQiChongZhu wuQiChongZhu = (UserWuQiChongZhu) message.obj;
                    if (null != wuQiChongZhu) {
                        userWuQiChongZhu = wuQiChongZhu;
                        lsNumber = userWuQiChongZhu.getLsnumber();
                        gwNumber = userWuQiChongZhu.getGwnumber();
                        shNumber = userWuQiChongZhu.getShnumber();
                        setUserBagNumber();
                        if (null == adatper) {
                            adatper = new ZhuangbeiChongZhuGriveViewAdapter(ZhuangBeiTuPo.this);
                        }
                        adatper.setChongZhu(userWuQiChongZhu);
                        gvBag.setAdapter(adatper);
                    } else {
                        ToastUtil.toast(ZhuangBeiTuPo.this, "服务器炸了");
                    }
                    break;
                case 2:
                    User user = (User) message.obj;
                    if (null != user) {
                        GeneralDialog("重铸详情", user.getStw(), 0);
                        ZhuangBeiYuLan(Login.getUser().getUsernumber());
                        bag_id = 0;
                        tvName.setText("");
                        tvDj.setText("");
                        tvShuoMing.setText("");
                        bagi = 0;
                    } else {
                        ToastUtil.toast(ZhuangBeiTuPo.this, "服务器炸了");
                    }
                    break;
            }
            return false;
        }
    });

    private void setUserBagNumber() {
        tvHaveGW.setText("拥有：" + gwNumber + "");
        tvHaveLS.setText("拥有：" + lsNumber + "");
        tvHaveSH.setText("拥有：" + shNumber + "");
        tvShengJi.setText("成功率：" + yesNumber);
        tvJiangJ.setText("降级率：" + noNumber);
    }

    public void GeneralDialog(String head, String content, final int num) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */

        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setTitle(head)
                .setMessage(content)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        if (num == 1) {
                            //处理事件
                            ZhuangBeiChongZhu(Login.getUser().getUsernumber(), bag_id, addls, addgw, addsh);
                        } else {
                            //消息提示

                        }

                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void GeneralDialogDingJia(final Context context, String title, final int id) {
        /**
         * 编辑框
         * 1标题
         */
        AlertDialog dialog;
        final EditText editText = new EditText(context);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        InputFilter[] filters = {new InputFilter.LengthFilter(9)};
        editText.setFilters(filters);
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(editText)
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (editText.getText().toString().trim().equals("") || editText.getText().length() >= 16) {
                            dialogInterface.dismiss();
                            ToastUtil.toast(context, "输入有误");
                            return;
                        }
                        int Num = Integer.parseInt(editText.getText().toString().trim());
                        switch (id) {
                            case 0:
                                //怪物肉
                                if (Num * 500 > gwNumber) {
                                    ToastUtil.toast(getApplicationContext(), "数量不足");
                                    return;
                                } else {
                                    addgw = Num * 500;
                                    tvTianJiaGW.setText(addgw + "个");
                                    yesNumber = 20+addgw/500+addls*10;
                                    if (yesNumber  >= 80) {
                                        yesNumber = 80;
                                    }
                                }
                                break;
                            case 1:
                                //灵石
                                if (Num > lsNumber) {
                                    ToastUtil.toast(getApplicationContext(), "数量不足");
                                    return;
                                } else {
                                    addls = Num;
                                    tvTianJiaLS.setText(addls + "个");
                                    yesNumber = 20+addgw/500+addls*10;
                                    if (yesNumber  >= 80) {
                                        yesNumber = 80;
                                    }
                                }
                                break;
                            case 2:
                                //石魂
                                if (Num > shNumber) {
                                    ToastUtil.toast(getApplicationContext(), "数量不足");
                                    return;
                                } else {
                                    addsh = Num;
                                    tvTianJiaSH.setText(addsh + "个");
                                    if (100 - Num * 5 <= 0) {
                                        noNumber = 0;
                                    } else {
                                        noNumber = 100 - Num * 5;
                                    }
                                }
                                break;
                        }
                        setUserBagNumber();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnTuJianGW:
                GeneralDialogDingJia(ZhuangBeiTuPo.this, "输入次数：（当前拥有:" + gwNumber + "  每500/次）", 0);
                break;
            case R.id.btnTuJianLS:
                GeneralDialogDingJia(ZhuangBeiTuPo.this, "输入次数：（当前拥有:" + lsNumber + "  每1/次）", 1);
                break;
            case R.id.btnTuJianSH:
                GeneralDialogDingJia(ZhuangBeiTuPo.this, "输入次数：（当前拥有:" + shNumber + "  每1/次）", 2);
                break;
            case R.id.tvTuPo:
                if (bag_id == 0) {
                    ToastUtil.toast(ZhuangBeiTuPo.this, "还未选择装备");
                } else {
                    String content = "";
                    if (rbtn1.isChecked()){
                        content="纯阴火";
                    }else{
                        content="至阳火";
                    }
                        GeneralDialog("重铸装备", "确定使用"+content+"重铸装备?\n注：300数值以上的装备无法上架交易所", 1);
                }
                break;
        }
    }
}